package com.ttbj.freight.component.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ttbj.core.dao.redis.IRedisValueBaseDao;
import com.ttbj.core.util.security.aes.AESUtils;
import com.ttbj.freight.component.OrderStateComponent;
import com.ttbj.product.bean.dto.OrderStateBo;

@Component
public class OrderStateComponentImpl implements OrderStateComponent {

	public static final Logger logger =LoggerFactory.getLogger(OrderStateComponentImpl.class);
	@Autowired
	IRedisValueBaseDao<OrderStateBo> redisValueDao = null;

	@Override
	public void addOrderStateCache(Long orderId, Integer willDriverNum) {
		String Key = AESUtils.encrypt(String.valueOf(orderId));
		redisValueDao.set(Key, new OrderStateBo(willDriverNum,orderId, 0, 0, 0));
	}
	
	

	@Override
	public OrderStateBo getOrderStateCache(Long orderId) {
		String Key = AESUtils.encrypt(String.valueOf(orderId));
		System.out.println(redisValueDao.get(Key));
		return redisValueDao.get(Key);
	}
	
	
	
	@Override
	public void delOrderStateCache(Long orderId) {
		String Key = AESUtils.encrypt(String.valueOf(orderId));
		redisValueDao.del(Key);
	}

	@Override
	public void updateOrderStateCacheAdd(Long orderId, Integer stateFlag) {
		OrderStateBo orderStateBo = getOrderStateCache(orderId);
		if(null == orderStateBo){
			logger.info("缓存中不存在订单状态对象");
			return;
		}
		switch (stateFlag) {
		case 1:
			orderStateBo.setReciverOrderNum(orderStateBo.getReciverOrderNum() + 1);
			break;
		case 2:
			orderStateBo.setReciverGoodsNum(orderStateBo.getReciverGoodsNum() + 1);
			break;
		case 3:
			orderStateBo.setDeliverGoodsNum(orderStateBo.getDeliverGoodsNum() + 1);
			break;
		default:
			break;
		}
		setOrderStateCache(orderId, orderStateBo);
	}

	 

	@Override
	public boolean matchStateFull(Long orderId, Integer stateFlag) {
		OrderStateBo orderStateBo = getOrderStateCache(orderId);
		if(null == orderStateBo){
			logger.info("缓存中不存在订单状态对象");
			return false;
		}
		Integer num = null;
		switch (stateFlag) {
		case 1:
			num = orderStateBo.getReciverOrderNum();
			break;
		case 2:
			num = orderStateBo.getReciverGoodsNum();
			break;
		case 3:
			num = orderStateBo.getDeliverGoodsNum();
			break;
		default:
			break;
		}
		//完成车辆和总车辆数做对比
		return orderStateBo.getNormNum().equals(num);
	}



	@Override
	public void setOrderStateCache(Long orderId, OrderStateBo bo) {
		String Key = AESUtils.encrypt(String.valueOf(orderId));
		redisValueDao.set(Key, bo);
	}

}
